package me.lorenzop.arduinolink;

import java.io.IOException;
import java.util.HashMap;
import java.util.Map;
import java.util.logging.Logger;

import org.bukkit.ChatColor;
import org.bukkit.configuration.file.FileConfiguration;
import org.bukkit.plugin.java.JavaPlugin;

public class ArduinoLink extends JavaPlugin {


	// isDev is for testing mode only
	public final boolean isDev = true;
	private      boolean isOk  = false;

	public static String logPrefix  = "[ArduinoLink] ";
	public static String chatPrefix = ChatColor.DARK_GREEN+"["+ChatColor.WHITE+"ArduinoLink"+ChatColor.DARK_GREEN+"] ";
	public static final Logger log = Logger.getLogger("Minecraft");

	public static Metrics metrics;

	public FileConfiguration Config;

	// tcp servers/clients
	public HashMap<String, ArduinoNetworking> networking = new HashMap<String, ArduinoNetworking>();


	public ArduinoLink() {
	}


	public void onEnable() {
//		log.info(logPrefix + "ArduinoLink is initializing.");
		if(isDev) {
			getServer().getConsoleSender().sendMessage(ChatColor.RED+"******************************");
			getServer().getConsoleSender().sendMessage(ChatColor.RED+"*** Running in dev mode!!! ***");
			getServer().getConsoleSender().sendMessage(ChatColor.RED+"***    for testing only    ***");
			getServer().getConsoleSender().sendMessage(ChatColor.RED+"******************************");
		}

		// Command listener
//		getCommand("wa").setExecutor(ArduinoLinkCommandsListener);

		// init configs
		Config = getConfig();
//		initConfig();

//		onLoadMetrics();

//		PluginManager pm = getServer().getPluginManager();
//		pm.registerEvents(new WebAuctionPlayerListener(this), this);
//		pm.registerEvents(new WebAuctionBlockListener (this), this);
//		pm.registerEvents(new WebAuctionServerListener(this), this);
		isOk = true;
	}
	public boolean isOk() {return isOk;}


	public void onDisable() {
//		for(Map.Entry<String, ArduinoNetworking> entry : networking.entrySet()) {
//			networking.put(entry.getKey(), null);
//		}
		networking.clear();
		log.info(logPrefix + "Disabled, bye for now :-)");
	}


	public boolean onLoadConfig() {
		return true;
	}


	private void initConfig() {
//		Config.addDefault("MySQL.Host",						"localhost");
		Config.options().copyDefaults(true);
		saveConfig();
	}


	// format chat colors
	public static String ReplaceColors(String text){
		return text.replaceAll("&([0-9a-fA-F])", "\247$1");
	}


	public void onLoadMetrics() {
		// usage stats
		try {
			Metrics.isDev = isDev;
			metrics = new Metrics(this);
			metrics.setBaseUrl("http://metrics.poixson.com");
			if(metrics.isOptOut()) {
				log.info(logPrefix+"Plugin metrics are disabled, you bum");
				return;
			}
			log.info(logPrefix+"Starting metrics");
			// Create graphs linked devices
			Metrics.Graph linksGraph = metrics.createGraph("Arduino Links");
			// linked controllers
			Metrics.Plotter plotterLinkedControllers = new Metrics.Plotter("ArduinoGC") {
				@Override
				public int getValue(){
					return networking.size();
				}
			};
			// linked devices
			Metrics.Plotter plotterLinkedDevices = new Metrics.Plotter("Auctions") {
				@Override
				public int getValue(){
					int count = 0;
					for(Map.Entry<String, ArduinoNetworking> entry : networking.entrySet()) {
						count += entry.getValue().countDevices();
					}
					return count;
				}
			};
			// total links
			linksGraph.addPlotter(plotterLinkedControllers);
			linksGraph.addPlotter(plotterLinkedDevices);
			metrics.start();
		} catch (IOException e) {
			// Failed to submit the stats :-(
			if(isDev) {
				log.severe(e.getMessage());
				e.printStackTrace();
			}
		}
	}


}
